﻿using System.Linq;
using System.Web.Mvc;
using BLL.Security;
using BLL.Services.Books;
using BLL.Services.Ratings;
using Web.Infrastructure;
using Web.Models;

namespace Web.Controllers
{
    public partial class HomeController : FrontendController
    {
        private readonly IBookService _bookService;

        private readonly IEnvironmentContext _environmentContext;

        public HomeController(IBookService bookService, IEnvironmentContext environmentContext)
        {
            _bookService = bookService;
            _environmentContext = environmentContext;
        }
        public virtual ActionResult Index()
        {
            return BackBoneView(MVC.Home.Views.Index);
        }

        public virtual ActionResult IndexTemplate()
        {
            return BackBoneView(MVC.Home.Views.Index);
        }

        public virtual ActionResult IndexData()
        {
            var model = new HomeModel();
            model.NewBooks = _bookService.GetNewBooks().Select(book => new HomeModel.NewBookModel(book));
            var userId = (_environmentContext.User != null ? (int?)_environmentContext.User.UserId : null);
            model.BestBooks = _bookService.GetBestBooks(userId);
            return BackBoneJson(model);
        }
    }
}
